<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;


class BaseController
{
    public function resultResponse($sdata){
        $data = isset($sdata["data"])? $sdata["data"]:null;
        $code_code = isset($sdata["code"])? $sdata["code"]:10000;
        $message = $this->get_code($code_code);
        $rec_data = [
            'code'    => $code_code,
            'status'  => "SUCCESS",
            'message'  => $message,
            'content' => isset($sdata["data"]) ? $data: (object)[],
        ];

        switch (true)
        {
            case is_numeric($code_code)&&$code_code!=10000:
                $rec_data['status'] =         'FAIL';
                break;
        }
        return $rec_data;
    }

    public function get_code($code_code=''){
        $code = [
            '10000' => '成功',
            '20001' => 'open_id不能为空！',
            '20002' => '授权失败！',
            '20003' => 'order_id不能为空！',
            '20004' => '暂无数据',
            '20030' => '当前章节没有购买的！',
            '20040' => '任务ID不能外空！',
            '20041' => '任务不存在',
            '20042' => '当前任务已经领取',
            '20043' => '不满足条件',
            '20050' => '商品ID不能为空！',
            '20060' => '缺少必要参数！',
            '20061' => '余额不足！',
            '20062' => '网络不稳定，请稍后重试',
            '20063' => '订单已经发货',
        ];
        return isset($code[$code_code])? $code[$code_code]:'';
    }

    public function get_applet_id(){
        # 获取肚下程序applet_id
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        return $headers["applet_id"][0];
    }
    public function platform_type(){
        # 获取肚下程序applet_id
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        echo "headers".print_r($headers).PHP_EOL;
        return $headers["platform_type"][0];
    }

    public function get_ip(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
            return $headers['x-forwarded-for'][0];
        } elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
            return $headers['x-real-ip'][0];
        }elseif(isset($headers['remoteip'][0]) && !empty($headers['remoteip'][0])) {
            return $headers['remoteip'][0];
        }
        $serverParams = $request->getServerParams();
        return $serverParams['remote_addr'] ?? '';
    }

    public function get_user_ua(){
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        return isset($headers["user-agent"][0]) ? $headers["user-agent"][0] : "";
    }

    public function get_user_info_rel($user_info){
        $recd["id"] = $user_info->id;
        $recd["xc_open_id"] = $user_info->xc_open_id;
        $recd["gz_open_id"] = $user_info->gz_open_id;
        $recd["headimg"] = $user_info->headimg;
        $recd["username"] = !empty($user_info->username) ? $user_info->username : "短剧用户";
        $recd["is_vip"] = $user_info->is_vip;
        $recd["vip_effective_time"] = $user_info->vip_effective_time;
        $recd["vip_effective_time_time"] = $user_info->vip_effective_time_time;
        $recd["ml_coin"] = $user_info->ml_coin;
        $recd["give_coin"] = $user_info->give_coin;
        $recd["spread_id"] = $user_info->spread_id;
        $recd["ios_payment"] = $user_info->ios_payment;
        $recd["coin_name"] = "看币";
        return $recd;
    }

}